package com.eqdd.yiqidian.ui.yiqiyue;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.internal.MDRootLayout;
import com.coorchice.library.SuperTextView;
import com.eqdd.library.percentUtil.WindowUtil;
import com.eqdd.yiqidian.ArticleInfoActivityCustom;
import com.eqdd.yiqidian.ListItem85Custom;
import com.eqdd.yiqidian.ListItem91Custom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows;
import com.eqdd.yiqidian.bean.rebuild.YueArticleBean;
import com.eqdd.yiqidian.bean.rebuild.YueCommentBean;
import com.eqdd.yiqidian.box.ItemDecoration.DashDividerLineItemDecoration;
import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.box.span.NoLineClickSpan;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.CustomLoadViewFactory;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.share.JShareDialog;
import com.eqdd.yiqidian.share.LinkShareBuilder;
import com.eqdd.yiqidian.ui.InputDialogActivity;
import com.eqdd.yiqidian.ui.mine.UserCardActivity;
import com.eqdd.yiqidian.utils.ClickUtil;
import com.eqdd.yiqidian.utils.CollectionUtil;
import com.eqdd.yiqidian.utils.CopyUtil;
import com.eqdd.yiqidian.utils.DensityUtil;
import com.eqdd.yiqidian.utils.HttpUtil;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.ListUtil;
import com.eqdd.yiqidian.utils.PayUtil;
import com.eqdd.yiqidian.utils.ScrollLoadMoreUtil;
import com.eqdd.yiqidian.utils.ShareUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;
import com.xujiaji.happybubble.BubbleDialog;
import com.xujiaji.happybubble.BubbleLayout;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 17-12-15  下午5:23.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class ArticleInfoActivity extends BaseActivity {

    private ArticleInfoActivityCustom dataBinding;
    private int id;
    private int pageNum;
    private int parentId;
    private int commentId;
    private boolean isMyComment;
    private String talkTo;
    private String talkContent;
    private MVCCoolHelper<List<YueCommentBean>> mvcHelper;
    private BubbleDialog bubbleDialog;
    private MaterialDialog moreDialog;
    private Boolean isAttension;
    private Boolean isZan;
    private BottomSheetDialog shareDialog;
    private boolean isFirst = true;
    private MaterialDialog reportDialog;
    private int firstCommentId;
    private String parentUserGuid;
    private int type;
    private SlimAdapterEx<YueCommentBean> slimTalkAdapter;
    private boolean isCommentUp;
    private SlimAdapterEx slimAdapterEx;
    private int dealPos;

    public static String BASE_ARTICLE_LINK = "https://www.eqidd.com/chuangkeApace/html/circleDetails.html?id=";

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.article_info);
        id = getIntent().getIntExtra(Config.ID, 0);
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(this)
                .setLeftPadding(0)
                .setRightPadding(0)
                .setHeadHeight(0));
        slimAdapterEx = SlimAdapterEx.create().registerDefault(R.layout.list_item_87, new SlimInjector<YueCommentBean>() {
            @Override
            public void onInject(YueCommentBean data, IViewInjector injector) {
                ArrayList<YueCommentBean> yueCommentBeans = new ArrayList<>();
                getAllComment(data.getList(), yueCommentBeans);
                RecyclerView recyclerView = (RecyclerView) injector.findViewById(R.id.recyclerView);
                recyclerView.setLayoutManager(new LinearLayoutManager(ArticleInfoActivity.this));
                recyclerView.addItemDecoration(new DashDividerLineItemDecoration(ArticleInfoActivity.this));
                SlimAdapterEx<YueCommentBean> slimAdapterExCommentChild = SlimAdapterEx.create().register(R.layout.list_item_88, new SlimInjector<YueCommentBean>() {
                    @Override
                    public void onInject(YueCommentBean data, IViewInjector injector) {
                        injector.with(R.id.tv_content, view -> {
                            TextView textView = (TextView) view;
                            SpannableString spStr = new SpannableString(data.getUpname() + ":" + " @" + data.getParentUPname() + " " + data.getContent());
                            NoLineClickSpan firstSpan = new NoLineClickSpan("#3423ff", v -> {

                                Bundle bundle = new Bundle();
                                bundle.putString(Config.GUID, data.getUserGuid());
                                startActivity(UserCardActivity.class, bundle);
                            });
                            NoLineClickSpan secondSpan = new NoLineClickSpan("#3423ff", v -> {
                                Bundle bundle = new Bundle();
                                bundle.putString(Config.GUID, data.getParentUserGuid());
                                startActivity(UserCardActivity.class, bundle);
                            });
                            spStr.setSpan(firstSpan, 0, data.getUpname().length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                            spStr.setSpan(secondSpan, spStr.toString().indexOf("@"), spStr.toString().indexOf("@") + 1 + data.getParentUPname().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                            textView.setMovementMethod(LinkMovementMethod.getInstance());
                            textView.setText(spStr);
                        });
                    }
                }).attachTo(recyclerView).updateData(yueCommentBeans);
                ItemClickSupport.addTo(recyclerView)
                        .setOnItemClickListener((recyclerView1, position, v) -> {
                            YueCommentBean dataItem = slimAdapterExCommentChild.getDataItem(position);
                            isMyComment = dataItem.getUserGuid().equals(AppConstent.user.getGuid());
                            parentId = dataItem.getId();
                            parentUserGuid = dataItem.getUserGuid();
                            firstCommentId = data.getId();
                            commentId = dataItem.getId();
                            talkTo = dataItem.getUpname();
                            talkContent = dataItem.getContent();
                            type = 2;
                            slimTalkAdapter = slimAdapterExCommentChild;
                            isCommentUp = false;
                            dealPos = position;
                            showMoreDialog();
                        })
                        .setOnItemLongClickListener((recyclerView1, position, v) -> {
                            YueCommentBean dataItem = slimAdapterExCommentChild.getDataItem(position);
                            showReplyContent(v, dataItem.getFirstComment());
                            return true;
                        });
                injector.imageCircle(R.id.iv_head, data.getIphoto())
                        .text(R.id.tv_name, data.getUpname())
                        .text(R.id.tv_time, data.getCreateTime())
                        .text(R.id.stv_comment, data.getCommentCount() + "")
                        .text(R.id.stv_zan, data.getZanCount() + "")
                        .enable(R.id.stv_zan, !data.isIsZan())
                        .visibility(R.id.tv_more_comment, data.getCommentCount() > 3 ? View.VISIBLE : View.GONE)
                        .text(R.id.tv_more_comment, String.format("展开更多%d条评论", data.getCommentCount()))
                        .clicked(R.id.tv_more_comment, v -> {

                            Bundle bundle = new Bundle();
                            bundle.putSerializable(Config.BEAN, data);
                            startActivity(CommentListActivity.class, bundle);
                        })
                        .with(R.id.stv_zan, view -> {
                            SuperTextView superTextView = (SuperTextView) view;
                            if (data.isIsZan()) {
                                superTextView.setDrawable(getResources().getDrawable(R.mipmap.ic_heart_red));
                            } else {
                                superTextView.setDrawable(getResources().getDrawable(R.mipmap.ic_heart_hint));
                                ClickUtil.click(superTextView, () -> {
                                    HttpUtil.zanComment(data.getId(), () -> {
                                        data.setIsZan(true);
                                        superTextView.setDrawable(getResources().getDrawable(R.mipmap.ic_heart_red));
                                        superTextView.setEnabled(false);
                                        superTextView.setText(data.getZanCount() + 1 + "");

                                    });
                                });
                            }
                        })
                        .with(R.id.stv_comment, view -> {
                            SuperTextView superTextView = (SuperTextView) view;
                            superTextView.setDrawable(getResources().getDrawable(R.mipmap.ic_comment_hint));
                        })
                        .text(R.id.tv_content, data.getContent())
                        .clicked(R.id.stv_comment, v -> {
                            parentId = data.getId();
                            talkTo = data.getUpname();
                            parentUserGuid = data.getUserGuid();
                            firstCommentId = data.getId();
                            slimTalkAdapter = slimAdapterExCommentChild;
                            isCommentUp = true;
                            dealPos = slimAdapterEx.getData().indexOf(data);
                            talkTo();
                        })
                        .clicked(R.id.tv_content, v -> {
                            isMyComment = data.getUserGuid().equals(AppConstent.user.getGuid());
                            parentId = data.getId();
                            parentUserGuid = data.getUserGuid();
                            firstCommentId = data.getId();
                            commentId = data.getId();
                            talkTo = data.getUpname();
                            talkContent = data.getContent();
                            type = 1;
                            slimTalkAdapter = slimAdapterExCommentChild;
                            isCommentUp = true;
                            dealPos = slimAdapterEx.getData().indexOf(data);
                            showMoreDialog();

                        });

            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<YueCommentBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<HttpResultRows<YueCommentBean>>post(HttpConfig.BASE_URL + HttpConfig.YUE_COMMENT_LIST)
                        .params("page", pageNum)
                        .params("articleId", id)
                        .converter(new JsonConverter<HttpResultRows<YueCommentBean>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<>())
                        .flatMap(listResultBean -> {
                            pageNum = listResultBean.getItems().getPage();
                            if (listResultBean.getStatus() == 200) {
                                return Flowable.just(listResultBean.getItems() == null ? new ArrayList<YueCommentBean>() : listResultBean.getItems().getRows());
                            } else {
                                ToastUtil.showShort(listResultBean.getMsg());
                                return Flowable.just(new ArrayList<>());
                            }
                        });
            }
        });
        MVCCoolHelper.setLoadViewFractory(new CustomLoadViewFactory().setShowEmpty(false));
        mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();
        dataBinding.coolRefreshView.setEnabled(false);
        dataBinding.coolRefreshView.setNestedScrollingEnabled(false);
        dataBinding.richEditor.setInputEnabled(false);
        dataBinding.richEditor.setPadding(15, 15, 15, 15);
        dataBinding.recyclerView.setNestedScrollingEnabled(false);
        ClickUtil.click(dataBinding.ivBack, super::onBackPressed);
        ScrollLoadMoreUtil.loadMore(dataBinding.nestedScrollView, () -> {
            if (dataSource.hasMore()) {
                mvcHelper.loadMore();
            }
        });

    }


    private void initTopDialogAndClick(YueArticleBean items) {
        if (bubbleDialog == null) {
            ListItem85Custom inflate = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.list_item_85, null, false);
            inflate.recyclerView.setLayoutManager(new LinearLayoutManager(this));
            inflate.recyclerView.addItemDecoration(new SectionDividerItemDecoration(this)
                    .setLeftPadding(DensityUtil.percentW(20))
                    .setRightPadding(DensityUtil.percentW(20))
                    .setLastBootomHeght(0)
                    .setHeadHeight(0));
            ArrayList<String> options = new ArrayList<>();
            options.add("分享");
            options.add("收藏");
            options.add("举报");
            if (items.getUserGuid().equals(AppConstent.user.getGuid())) {
                options.add("编辑");
                options.add("删除");
            }
            SlimAdapterEx.create().register(R.layout.list_item_86, new SlimInjector<String>() {
                @Override
                public void onInject(String data, IViewInjector injector) {
                    injector.text(R.id.tv_content, data);
                }
            }).attachTo(inflate.recyclerView).updateData(options);
            ItemClickSupport.addTo(inflate.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        bubbleDialog.hide();
                        switch (position) {
                            case 0:
                                showShareDialog(yueArticleBean);
                                break;
                            case 1:
                                CollectionUtil.INSTANCE.chooseLink(
                                        items.getTitle(),
                                        BASE_ARTICLE_LINK + yueArticleBean.getId(),
                                        "易企阅",
                                        items.getUserGuid(),
                                        items.getId().toString());
                                break;
                            case 2:
                                if (reportDialog == null) {
                                    reportDialog = new MaterialDialog.Builder(this)
                                            .items("广告或垃圾信息", "转载", "其它")
                                            .itemsCallbackSingleChoice(0, (dialog, view, which, text) -> {
                                                HttpUtil.reportArticle(id, text.toString(), ((TextView) reportDialog.getView().findViewById(R.id.et_content)).getText().toString().trim(), () -> {

                                                });
                                                return true;
                                            })
                                            .content("请选择举报类型")
                                            .positiveText("确定")
                                            .negativeText("取消")
                                            .build();
                                    if (reportDialog.getView() instanceof MDRootLayout) {
                                        EditText editView = (EditText) DataBindingUtil.inflate(getLayoutInflater(), R.layout.list_item_90, null, false).getRoot();
                                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(800, 130);
                                        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
                                        editView.setLayoutParams(layoutParams);
                                        editView.setMaxHeight(300);
                                        ((LinearLayout) ((ViewGroup) reportDialog.getView()).getChildAt(1)).addView(editView);
                                    }
                                }
                                reportDialog.show();
                                break;
                            case 3:
                                Bundle bundle = new Bundle();
                                bundle.putInt(Config.ID, id);
                                startActivityForResult(AddArticleActivity.class, bundle, Config.UPDATE_ARTICLE);
                                break;
                            case 4:
                                HttpUtil.deleteArticle(ArticleInfoActivity.this, id, this::finish);
                                break;
                        }
                    });
            bubbleDialog = new BubbleDialog(this)
                    .addContentView(inflate.getRoot())
                    .setClickedView(dataBinding.ivOption)
                    .setPosition(BubbleDialog.Position.BOTTOM)
                    .calBar(true);
            ClickUtil.click(dataBinding.ivOption, () -> {
                bubbleDialog.show();
            });
        }
    }

    private void showReplyContent(View view, String content) {
        ListItem91Custom inflate = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.list_item_91, null, false);
        inflate.tvContent.setText(content);
        BubbleLayout bl = new BubbleLayout(this);
        bl.setBubbleColor(Color.parseColor("#744e37"));
        bl.setShadowColor(Color.RED);
        bl.setLookLength(DensityUtil.percentW(15));
        bl.setLookWidth(DensityUtil.percentW(20));
        Rect viewRect = new Rect();
        view.getGlobalVisibleRect(viewRect);
        new BubbleDialog(this)
                .setClickedView(view)
                .addContentView(inflate.getRoot())
                .setPosition(viewRect.centerY() > (WindowUtil.csh >> 1) ? BubbleDialog.Position.TOP : BubbleDialog.Position.BOTTOM)
                .calBar(true)
                .setOffsetY(DensityUtil.percentW(viewRect.centerY() > (WindowUtil.csh >> 1) ? 15 : -15))
                .setBubbleLayout(bl)
                .setTransParentBackground()
                .show();
    }

    private void talkTo() {
        Bundle bundle = new Bundle();
        bundle.putString(Config.TALK_TO, talkTo);
        startActivityForResult(InputDialogActivity.class, bundle, Config.INPUT);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MVCCoolHelper.setLoadViewFractory(new CustomLoadViewFactory().setShowEmpty(true));
    }

    private void showMoreDialog() {
        if (moreDialog == null) {
            ArrayList<Object> objects = new ArrayList<>();
            objects.add("回复");
            objects.add("复制");
            objects.add("删除");
            SlimAdapterEx slimAdapterEx = SlimAdapterEx.create().registerDefault(R.layout.list_item_54, new SlimInjector() {
                @Override
                public void onInject(Object data, IViewInjector injector) {

                    injector.text(R.id.tv_content, (String) data);
                }
            }).updateData(objects);
            moreDialog = new MaterialDialog.Builder(this)
                    .adapter(slimAdapterEx, new LinearLayoutManager(this))
                    .build();
            moreDialog.getRecyclerView().addItemDecoration(new SectionDividerItemDecoration(this)
                    .setHeadHeight(0)
                    .setRightPadding(DensityUtil.percentW(40))
                    .setLeftPadding(DensityUtil.percentW(40))
                    .setLastBootomHeght(0));
            ItemClickSupport.addTo(moreDialog.getRecyclerView())
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        switch (position) {
                            case 0:
                                talkTo();
                                break;
                            case 1:
                                CopyUtil.copy(talkContent);
                                break;
                            case 2:
                                HttpUtil.deleteComment(ArticleInfoActivity.this, commentId, id, type, () -> {
                                    if (isCommentUp) {
                                        slimAdapterEx.getData().remove(dealPos);
                                        slimAdapterEx.notifyItemRemoved(dealPos);
                                    } else {
                                        slimTalkAdapter.getData().remove(dealPos);
                                        slimTalkAdapter.notifyItemRemoved(dealPos);
                                    }
                                });
                                break;
                        }
                        moreDialog.dismiss();
                    });
        }
        SlimAdapterEx slimAdapterExMore = (SlimAdapterEx) moreDialog.getRecyclerView().getAdapter();
        if (isMyComment && slimAdapterExMore.getData().size() == 2) {
            slimAdapterExMore.getData().add("删除");
            slimAdapterExMore.notifyItemInserted(2);
        } else if (!isMyComment && slimAdapterExMore.getData().size() == 3) {
            slimAdapterExMore.getData().remove(2);
            slimAdapterExMore.notifyItemRemoved(2);
        }
        moreDialog.show();

    }

    private void getAllComment(List<YueCommentBean> list, ArrayList<YueCommentBean> yueCommentBeans) {
        if (ListUtil.isEmpty(list))
            return;
        for (YueCommentBean yueCommentBean : list) {
            yueCommentBeans.add(yueCommentBean);
            getAllComment(yueCommentBean.getList(), yueCommentBeans);
        }
    }

    @Override
    public void setView() {
        OkGo.<HttpResult<YueArticleBean>>post(HttpConfig.BASE_URL + HttpConfig.YUE_ARTICLE_INFO)
                .params("articleId", id)
                .params("userGuid", AppConstent.user.getGuid())
                .execute(new DialogCallBack<HttpResult<YueArticleBean>>(ArticleInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<YueArticleBean>> response) {
                        HttpResult<YueArticleBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            refreshView(httpResult.getItems());
                        } else {
                            finish();
                        }
                    }
                });
    }

    private YueArticleBean yueArticleBean;

    private void refreshView(YueArticleBean items) {
        if (isFirst) {
            isFirst = false;
            ClickUtil.click(dataBinding.btReward, () -> PayUtil.pay(this, "易企点科技",
                    "赞赏" + items.getStaffName(), "2",
                    items.getUserGuid() + System.currentTimeMillis() + "",
                    items.getUserGuid() + System.currentTimeMillis() + ""));
            ClickUtil.click(dataBinding.tvComment, () -> {
                parentId = 0;
                firstCommentId = 0;
                parentUserGuid = "";
                talkTo = items.getStaffName();
                slimTalkAdapter = null;
                talkTo();
            });
            ClickUtil.click(dataBinding.ivShare, () -> showShareDialog(items));

            ImageUtil.setCircleImage(items.getIphoto(), dataBinding.ivHead);
            dataBinding.tvNameTop.setText(items.getStaffName());
            ImageUtil.setCircleImage(items.getIphoto(), dataBinding.ivHeadArticle);
            dataBinding.tvNameArticle.setText(items.getStaffName());
            isZan = items.isZan();
            if (!isZan) {
                ClickUtil.click(dataBinding.ivZan,
                        () -> HttpUtil.zanArticle(ArticleInfoActivity.this, id,
                                () -> {
                                    isZan = true;
                                    dataBinding.ivZan.setEnabled(false);
                                    refreshZanCount(yueArticleBean.getZanCount() + 1);
                                }));
            }
            getAttention(items);
            initTopDialogAndClick(items);
        }
        yueArticleBean = items;
        dataBinding.tvTitle.setText(items.getTitle());
        String html = items.getContent().replace("<pre>", "").replace("</pre>", "");
        dataBinding.richEditor.setHtml(html);
        dataBinding.stvMenu.setText("日记本");
        dataBinding.stvMenu.setDrawable(getResources().getDrawable(R.mipmap.ic_note_book));
        refreshCommentCount(items.getCommentCount());
        refreshZanCount(items.getZanCount());
    }

    private void getAttention(YueArticleBean items) {
        isAttension = items.isAttention();
        switchAttention();
        ClickUtil.click(dataBinding.btAttention, () -> {
            if (isAttension) {
                HttpUtil.cancleAttention(items.getUserGuid(), () -> {
                    isAttension = false;
                    switchAttention();
                });
            } else {
                HttpUtil.attention(items.getUserGuid(), () -> {
                    isAttension = true;
                    switchAttention();
                });
            }
        });
    }

    private void showShareDialog(YueArticleBean items) {
        LinkShareBuilder builder = new LinkShareBuilder(this);
        builder.setTitle(items.getTitle())
                .setId(yueArticleBean.getId().toString())
                .setText(items.getTextContent())
                .setUrl(ShareUtil.getArticleUrl(yueArticleBean.getId()))
                .setImageUrl(yueArticleBean.getHomeImage())
                .setSource("易企阅")
                .setSourceOwner(yueArticleBean.getUserGuid());
        new JShareDialog(builder, this).show();
    }

    private void switchAttention() {
        dataBinding.btAttention.setVisibility(View.VISIBLE);
        if (isAttension) {
            dataBinding.btAttention.setBackgroundResource(R.drawable.shape_16);
            dataBinding.btAttention.setTextColor(getResources().getColor(R.color.black));
            dataBinding.btAttention.setText("√ 已关注");
        } else {
            dataBinding.btAttention.setBackgroundResource(R.drawable.shape_2);
            dataBinding.btAttention.setTextColor(getResources().getColor(R.color.white));
            dataBinding.btAttention.setText("+ 关注");
        }
    }

    private void refreshCommentCount(int commentCount) {
        dataBinding.tvCommentCount.setVisibility(commentCount <= 0 ? View.INVISIBLE : View.VISIBLE);
        dataBinding.tvCommentCount.setText(commentCount >= 99 ? "99+" : commentCount + "");
    }

    private void refreshZanCount(int zanCount) {
        dataBinding.tvZanCount.setVisibility(zanCount <= 0 ? View.INVISIBLE : View.VISIBLE);
        dataBinding.tvZanCount.setText(zanCount >= 99 ? "99+" : zanCount + "");
        if (isZan) {
            dataBinding.ivZan.setImageResource(R.mipmap.ic_heart_red);
        } else {
            dataBinding.ivZan.setImageResource(R.mipmap.ic_heart_hint);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.INPUT && resultCode == Config.SUCCESS) {
            String content = data.getStringExtra(Config.CONTENT);
            HttpUtil.commentArticle(ArticleInfoActivity.this, id, parentId, content, parentUserGuid, firstCommentId, (commentBean) -> {
                if (slimTalkAdapter == null) {
                    slimAdapterEx.getData().add(0, commentBean);
                    slimAdapterEx.notifyItemInserted(0);
                } else if (isCommentUp) {
                    slimTalkAdapter.getData().add(0, (YueCommentBean) commentBean);
                    slimTalkAdapter.notifyItemInserted(0);
                } else {
                    slimTalkAdapter.getData().add((YueCommentBean) commentBean);
                    slimTalkAdapter.notifyItemInserted(slimTalkAdapter.getData().size() - 1);
                }
            });
        } else if (requestCode == Config.UPDATE_ARTICLE && resultCode == Config.UPDATE) {
            setView();
        }
    }
}
